package com.bawei.splash

import android.os.Handler
import android.os.Looper
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.RouterPath
import com.bawei.common.adapter.ImageBannerAdapter
import com.bawei.common.base.BaseActivity
import com.bawei.common.entity.BannerEntity
import com.bawei.splash.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer

class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private lateinit var mImageAdapter: ImageBannerAdapter
    private var imageData = mutableListOf<BannerEntity>()

    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initViews() {


        //判断是否是第一次打开APP
        var isFirst = MMKV.defaultMMKV().decodeBool("isFirst",true)
        if (isFirst) {
            MMKV.defaultMMKV().encode("isFirst",false)

            mDataBinding.clGuide.visibility = View.VISIBLE
            imageData.add(BannerEntity(R.drawable.wellcome_0,""))
            imageData.add(BannerEntity(R.drawable.wellcome_1,""))
            imageData.add(BannerEntity(R.drawable.wellcome_2,""))
            mImageAdapter = ImageBannerAdapter(imageData)
            mDataBinding.bannerGuide.setAdapter(mImageAdapter)
            //与当前Activity的生命周期关联
            mDataBinding.bannerGuide.addBannerLifecycleObserver(this)
            //设置指示器
            mDataBinding.bannerGuide.indicator = CircleIndicator(this)
            //设置切换效果
            mDataBinding.bannerGuide.addPageTransformer(AlphaPageTransformer())
            //设置Banner切换事件
            mDataBinding.bannerGuide.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }

                override fun onPageSelected(position: Int) {
                    //滑动到最后一个
                    if(position == imageData.size - 1){
                        mDataBinding.tvSkip.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvSkip.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }

            })

            //点击事件
            mDataBinding.tvSkip.setOnClickListener {
                startToMain()
            }

        } else {
            startToMain()
        }
    }

    private fun startToMain() {
        var token = MMKV.defaultMMKV().decodeString("token","")
        var path = if (!token.isNullOrEmpty()) RouterPath.PATH_MAIN else RouterPath.PATH_LOGIN
        ARouter.getInstance().build(path).navigation()
        this@SplashActivity.finish()
    }

    override fun onStart() {
        super.onStart()
        mDataBinding.bannerGuide.start()
    }

    override fun onStop() {
        super.onStop()
        mDataBinding.bannerGuide.stop()
    }

    override fun onDestroy() {
        super.onDestroy()
        mDataBinding.bannerGuide.destroy()
    }

}